package com.cgbl.cg.ui.main

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.blankj.utilcode.util.LogUtils
import com.cgbl.cg.R
import com.cgbl.cg.databinding.AppFragmentHomeBinding
import com.cgbl.cg.ui.home.*
import com.cgbl.cg.vm.main.HomeFragmentViewModel
import com.robot.base.base.activity.GLBaseFragment
import com.robot.base.configs.AppConstants
import com.robot.base.util.LiveDataBus
import me.goldze.mvvmhabit.BR

class HomeFragment : GLBaseFragment<AppFragmentHomeBinding, HomeFragmentViewModel>() {
    private lateinit var fragments: Array<Fragment>
    private lateinit var recommendFragment: HomeRecommendFragment
    private lateinit var followFragment: HomeFollowFragment
    private lateinit var workFragment: HomeWorksFragment
    private lateinit var courseFragment: HomeCourseFragment
    private lateinit var resourceFragment: HomeResourceFragment
    private lateinit var projectFragment: HomeProjectFragment
    private var lastIndex = 0
    override fun initContentView(
        inflater: LayoutInflater?,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): Int = R.layout.app_fragment_home

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        recommendFragment = HomeRecommendFragment()
        followFragment = HomeFollowFragment()
        workFragment = HomeWorksFragment()
        courseFragment = HomeCourseFragment()
        resourceFragment = HomeResourceFragment()
        projectFragment = HomeProjectFragment()
        fragments = arrayOf(
            recommendFragment, followFragment,
            workFragment, courseFragment, resourceFragment, projectFragment
        )

        switchFragment(0, 0)
    }

    override fun initViewObservable() {
        super.initViewObservable()
        viewModel.uc.tabType.observe(this) {
            switchFragment(lastIndex, it)
        }

        LiveDataBus.get().with(AppConstants.LiveKey.HOME_TAB, Int::class.java).observe(
            this
        ) {
            when(it){
                0->{
                    viewModel.clickSelectRecommend.execute()
                }
                1->{
                    viewModel.clickSelectFollow.execute()
                }
                2->{
                    viewModel.clickSelectWork.execute()
                }
                3->{
                    viewModel.clickSelectCourse.execute()
                }
                4->{
                    viewModel.clickSelectResource.execute()
                }
                5->{
                    viewModel.clickSelectProject.execute()
                }
            }
        }
    }

    /**
     * 切换fragment
     */
    private fun switchFragment(lastFragmentIndex: Int, index: Int) {
        val transaction = childFragmentManager.beginTransaction()
        //隐藏上个Fragment
        transaction.hide(fragments[lastFragmentIndex])
        if (!fragments[index].isAdded) {
            transaction.add(R.id.fragment_content, fragments[index])
        }
        transaction.show(fragments[index]).commitAllowingStateLoss()
        lastIndex = index
    }
}